package com.telenav.techtalk;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

/**
 * @author yxyao
 *
 */
public class MatrixActivity extends Activity{

	static String HELLO = "hello";
	
	private Bitmap bmp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(new MatrixView(this), new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.hawks_bay);
	}
	
	class MatrixView extends View{
		
		
		public MatrixView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onDraw(Canvas canvas) {
			TextPaint tp = new TextPaint();
			tp.setColor(0xFFFF0000);
			tp.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
			tp.setTextSize(20);
			tp.setAntiAlias(true);
			float textWidth = tp.measureText(HELLO);
			canvas.translate(0, 20);
			Matrix matrix = canvas.getMatrix();
			Matrix mirror = getMirrorMatrix();
			Matrix invertMirror = new Matrix();
			mirror.invert(invertMirror);
			//normal
			canvas.drawText(HELLO, 0, 0, tp);
			//translate y;
			matrix.preTranslate(0, 50);
			canvas.setMatrix(matrix);
			tp.setColor(0xFF00FF00);
			canvas.drawText(HELLO, 0, 0, tp);
			
			//translate x,y and mirror
			matrix.preTranslate(textWidth + textWidth, 0);
			matrix.preConcat(mirror);
			canvas.setMatrix(matrix);
			tp.setColor(0xFFFFFF00);
			canvas.drawText(HELLO, 0, 0, tp);
			
			//use invert matrix to remove the mirror.
			matrix.preConcat(invertMirror);
			canvas.setMatrix(matrix);
			tp.setColor(0xFFFF00FF);
			canvas.drawText(HELLO, 0, 0, tp);
			
			//translate x,y and rotate
			matrix.preRotate(90.0f);
			canvas.setMatrix(matrix);
			tp.setColor(0xFF0000FF);
			canvas.drawText(HELLO, 0, 0, tp);
			Matrix m = new Matrix();
			m.preTranslate(getWidth(), 200);
			m.preConcat(getMirrorMatrix());
			canvas.setMatrix(m);
			canvas.drawBitmap(bmp, 0,0,null);
		}
		
		private Matrix getMirrorMatrix(){
			Matrix m = new Matrix();
			m.setValues(new float[]{
				-1, 0,0,
				0, 1, 0,
				0, 0, 1,
			});
			return m;
		}
		
	}
}
